package cn.ehanghai.iot.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * @Author: xumf
 * @Date: 2019/6/27 8:58
 */
public class InviteCodeGnerateUtil {
    private static final String[] inviteChars = {"a", "c", "b", "d", "f", "e", "h", "i", "j", "k", "l", "m", "n", "o", "p"
            , "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    /**
     * 生成邀请码
     *
     * @param id
     * @return
     */
    public static Integer generateInviteCode(Long id) {
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat sdf2 = new SimpleDateFormat("YYMMdd");
        String dateStr = sdf2.format(date);
        return Integer.valueOf(dateStr + id.toString());
    }

    /**
     * 生成邀请码
     *
     * @param id
     * @return
     */
    public static String generateInviteCodeTwo(Long id) {
        String userIdStr = id.toString();
        int forSize = 6 - userIdStr.length();
        String randomStr = "";
        for (int i = 0; i < forSize; i++) {
            Random random = new Random();
            int randomIndex = random.nextInt(35);
            randomStr = randomStr + inviteChars[randomIndex];
        }
        return randomStr + userIdStr;
    }

    /**
     * 生成随机的6位数，短信验证码
     *
     * @return
     */
    public static String getMsgCode() {
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();
    }

    public static void main(String[] args) {
        String invitationCodeTwo = InviteCodeGnerateUtil.generateInviteCodeTwo(1l);
        Integer invitationCode = InviteCodeGnerateUtil.generateInviteCode(2l);
        System.out.println(invitationCodeTwo);
        System.out.println(invitationCode);
    }

}
